<html>
<head>
 <title>Gambling</title>
</head>

<body>
<center>
<h1>POI III Stage II Problem 3</h1>
<h1>Gambling</h1>
</center>
<p>A gambling machine consists of <i> n</i> generators of integers:
<b> G<sub>1</sub></b>,
..., <b> G</b><sub><b>n</b></sub>, where 1 &lt;= <i> n</i> &lt;= 1000. The generator
<b> G</b><sub><b>i</b></sub> can
generate integers only from the certain set <b> S</b><sub><b>i</b></sub> included in the
interval {1,..., <i>n</i>}, or 0 which means that the game is over. The <b> S</b><sub><b>i</b></sub>
can be an empty set. Let <i> k</i><sub><i>i</i></sub> be the number of elements of the set
<b> S<sub>i</sub></b>.
The sum of all the integers <i> k</i><i><sub>i</sub></i>, for i = 1,..., <i> n</i>, <i> 
</i>cannot exceed 12000.</p>
<p>While <b> G</b><sub><b>i</b></sub> is activated for the first time it
generates an integer from the set <b> S</b><sub><b>i</b></sub>. The next activating ends up in
generating an integer from the set <b> S</b><sub><b>i</b></sub>, which was not chosen
before. If there is no such an integer, <b> G</b><sub><b>i</b></sub> generates zero.</span></p>
<p><span lang="EN-AU">The machine starts with activating <b> G<sub>1</sub></b>. Then the
generators are activated according to the following rule :</p>
<ul>
  <li>In case when a generator generated a positive integer <i>r</i>, the next activated generator is
    <b> G<sub>r</sub></b>.</li>
  <li>If zero is generated the machine stops.</li>
</ul>
<p>The machine looses if the generator <b> G</b><sub><b>n</b></sub>
generates zero and the rest of the generators had exhausted their sets of
integers.</p>
<p>The machine is <b>well constructed</b> if it may generate
a sequence of integers ending with zero, but not leading to a defeat (i.e. a
sequence shorter then 1 + <i> ( the sum of integers k<sub>i</sub> for i = 1,...,n)</i>
).</p>
<h2>Task</h2>
<p>Write a program that:</p>
<ul dynamicoutline>
  <li>Reads from the text file HAZ.IN the description of the
    machine, i.e. the number of generators <i>n</i>, integers <i> k</i><sub><i>i</i></sub> and sets
    <b> S</b><sub><b>i</b></sub>,</li>
  <li>Verifies,
    whether the machine described in the input file is well constructed,</li>
  <li>if
    it is well constructed, writes a sequence of integers, which may be
    generated by the machine, is ended with zero, and does not lead to defeat,</li>
  <li>if not,
    writes one word NIE (<i>&quot;no&quot; in Polish</i>) in the text file
    HAZ.OUT.</li>
</ul>
<h2>Input</h2>
<p>In the first line of the text file HAZ.IN there is written
one positive integer <i> n</i> &lt;= 1000. This is the number of generators. In
the (<i>i</i> + 1)-st line (for <i> i</i> = 1,..., <i>n</i>) there is written an
integer <i>k</i><sub><i>i</i></sub>
followed by all the elements of the set <b> S</b><sub><b>i</b></sub> (written in
arbitrary order). The integers in each line are separated by single spaces.</p>
<h2>Output</h2>
<p>In the text file HAZ.OUT there should be written one word
NIE (if the machine isn't well constructed) or one line containing an
appropriate finite series of integers separated by single spaces. 
</p>
<h2>Example</h2>
<p>For the input file HAZ.IN:<br>
2<br>
2 1 2<br>
1 2</p>
<p>the correct answer is the output file HAZ.OUT:</p>
<p>2 2 0</p>
<p>For the input file HAZ.IN:<br>
2<br>
1 2<br>
0</p>
<p>the correct answer is the output file HAZ.OUT:<br>
NIE</p>
<p></p>

</body>

</html>
